//
//  ViewController.m
//  DemoTest
//
//  Created by DaShuEdu on 2017/4/24.
//  Copyright © 2017年 DaShuEdu. All rights reserved.
//

#import "ViewController.h"
#import "UIImage+image.h"
#import "MiddleView.h"
#import "TabBarControllerConfig.h"
#import "TabBarViewController.h"
#import "PhotoViewController.h"
#import "NONavOneViewController.h"
#import "DSTagsView.h"
#import "DSTagButton.h"
#import "UIButton+DSTag.h"
#import "Masonry.h"
#import "Son.h"
#define RGBAColor(r, g, b, a) [UIColor colorWithRed:(r)/255.0 green:(g)/255.0 blue:(b)/255.0 alpha:(a)/1.0]
@interface ViewController () <CAAnimationDelegate>

@property (nonatomic, strong) UIView *customV;
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UILabel *infoLabel;
@property (nonatomic, strong) NSString *titleStr;
@property (nonatomic, strong) DSTagsView *tagsV;
@property (nonatomic, strong) NSMutableArray *tagsArr;
@property (nonatomic, strong) UIButton *tagButton;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"首页";
    self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"bg.png"]];
    UIImage *image = [[UIImage imageNamed:@"android.png"] imageAntialias];
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(50, 20, 100, 100)];
    imageView.image = image;
    [self.view addSubview:imageView];
    
    MiddleView *middle = [[MiddleView alloc] initWithNewColor:[UIColor greenColor]]; //RGBAColor(241, 242, 243, 1)];
    middle.frame = CGRectMake(50, 130, 250, 100);
    [self.view addSubview:middle];
    
    UIButton *actionBtn = [[UIButton alloc] initWithFrame:CGRectMake(150, 20, 80, 40)];
    [actionBtn setTitle:@"TabBar" forState:UIControlStateNormal];
    [actionBtn setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    [actionBtn addTarget:self action:@selector(showTabBar:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:actionBtn];
    
    UIButton *photoBtn = [[UIButton alloc] initWithFrame:CGRectMake(150, 80, 80, 40)];
    [photoBtn setTitle:@"photo" forState:UIControlStateNormal];
    [photoBtn setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    [photoBtn addTarget:self action:@selector(showPhoto:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:photoBtn];
    
    self.tagButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.tagButton zhuanti:@"专题" remen:@"热门" liuxue:@"留学"];
    self.tagButton.frame = CGRectMake(260, 40, 42, 20);
    [self.tagButton addTarget:self action:@selector(customButton:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.tagButton];
    
    Person *son = [[Person alloc] init];
    son = [[Son alloc] init];
    NSLog(@"son:%@",son);
    
    self.tagsArr = [NSMutableArray arrayWithCapacity:5];
    [self.tagsArr addObject:@"你好啊"];
    
    [self addCustomView];
    
    UIButton *newButton = [UIButton buttonWithType:UIButtonTypeSystem];
    newButton.frame = CGRectMake(320, 240, 50, 40);
    [newButton setTitle:@"New" forState:UIControlStateNormal];
    [newButton addTarget:self action:@selector(newAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:newButton];
    
    NSString *helo = @"你好";
    NSLog(@"helo : %p - %@",helo,helo);
    NSString *hello = [helo copy];
    NSLog(@"hello: %p - %@",hello,hello);
    NSString *heloM = [helo mutableCopy];
    NSLog(@"heloM: %p - %@",heloM,heloM);
    NSLog(@"changed");
    helo = @"你好呀";
    NSLog(@"helo : %p - %@",helo,helo);
    NSLog(@"hello: %p - %@",hello,hello);
    NSLog(@"heloM: %p - %@",heloM,heloM);
    NSLog(@"1");
    dispatch_sync(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        NSLog(@"2");
    });
    NSLog(@"3");
}

- (void)addCustomView {
    self.titleStr = @"你今天还好吗，我不是很开心";
    
    self.customV = [[UIView alloc] init];
    self.titleLabel = [[UILabel alloc] init];
    self.infoLabel = [[UILabel alloc] init];
    [self.view addSubview:self.customV];
    [self.customV addSubview:self.titleLabel];
    [self.customV addSubview:self.infoLabel];
    
    self.titleLabel.backgroundColor = [UIColor yellowColor];
    self.infoLabel.backgroundColor = [UIColor orangeColor];
    self.customV.backgroundColor = [UIColor whiteColor];
    self.titleLabel.numberOfLines = 0;
    
    [_titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.bottom.mas_equalTo(self.customV);
        make.right.mas_equalTo(self.infoLabel.mas_left).offset(-12);
    }];
    
    [_infoLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.top.bottom.mas_equalTo(self.customV);
        make.width.mas_equalTo(58);
    }];
    
    [_customV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(240);
//        make.left.greaterThanOrEqualTo(self.view.mas_left).offset(15);
        make.left.mas_equalTo(150);
//        make.centerX.mas_equalTo(self.view);
        make.width.mas_equalTo(150);
        make.height.mas_equalTo(20);
    }];
    
    UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(10, 240, 100, 40)];
    [btn setTitle:@"change" forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(actionBtn) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn];
    
    self.tagsV = [[DSTagsView alloc] init];
    self.tagsV.backgroundColor = [UIColor greenColor];
    [self.view addSubview:self.tagsV];
    
//    self.tagsV.frame = CGRectMake(0, 0, 200, 20);
    [self.tagsV viewWithTagArr:self.tagsArr];
    
    [self.tagsV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(200);
        make.left.mas_equalTo(50);
        make.top.mas_equalTo(290);
        make.height.mas_equalTo(20);
    }];
}

- (void)newAction:(UIButton *)sender
{
    NONavOneViewController *one = [[NONavOneViewController alloc] init];
    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:one];
    [self presentViewController:nav animated:YES completion:nil];
}

static int i = 0;

- (void)actionBtn {
    NSLog(@"hahaha");
    CGPoint originPoint = self.tagButton.frame.origin;
    CGFloat btnW = self.tagButton.frame.size.width/2.0;
    NSLog(@"originPoint:%@",NSStringFromCGPoint(originPoint));
    CAKeyframeAnimation *animation = [CAKeyframeAnimation animation];
    animation.keyPath = @"position";
    animation.duration = 0.75;
    animation.delegate = self;
    animation.values = @[
                         [NSValue valueWithCGPoint:CGPointMake(originPoint.x+btnW, originPoint.y)],
                         [NSValue valueWithCGPoint:CGPointMake(originPoint.x+btnW, originPoint.y+40)],
                         [NSValue valueWithCGPoint:CGPointMake(originPoint.x+btnW, originPoint.y+20)]
                         ];
    
    animation.timingFunctions = @[
                                  [CAMediaTimingFunction functionWithName: kCAMediaTimingFunctionEaseIn],
                                  [CAMediaTimingFunction functionWithName: kCAMediaTimingFunctionEaseOut]
                                  ];
    
    animation.keyTimes = @[@0.0, @0.5, @0.75];
    //apply animation
    self.tagButton.layer.position = CGPointMake(originPoint.x+btnW, originPoint.y+20);
    [self.tagButton.layer addAnimation:animation forKey:nil];
    
    if (self.titleStr.length < i) {
        return;
    }
    _titleLabel.text = [self.titleStr substringFromIndex:i]; //你今天还好吗，我不是很开心
    _infoLabel.text = @"呵呵";
    NSDictionary *attribute = @{NSFontAttributeName:self.titleLabel.font};
    CGSize infoSize = [_infoLabel.text sizeWithAttributes:attribute];
    CGSize size = CGSizeMake(200-infoSize.width, MAXFLOAT);
    CGSize labelSize = [self.titleLabel.text boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin |
                        NSStringDrawingUsesFontLeading |
                        NSStringDrawingTruncatesLastVisibleLine attributes:attribute context:nil].size;
    if (labelSize.height>21) {
        // 多行
        [_infoLabel mas_updateConstraints:^(MASConstraintMaker *make) {
            make.width.mas_equalTo(infoSize.width+1);
        }];
        [_customV mas_updateConstraints:^(MASConstraintMaker *make) {
            make.width.mas_equalTo(200);
            make.height.mas_equalTo(labelSize.height+1);
        }];
        
    } else {
        // 单行
        [_infoLabel mas_updateConstraints:^(MASConstraintMaker *make) {
            make.width.mas_equalTo(infoSize.width+1);
        }];
        [_customV mas_updateConstraints:^(MASConstraintMaker *make) {
            make.width.mas_equalTo(labelSize.width+infoSize.width+2+12);
            make.height.mas_equalTo(infoSize.height+1);
        }];
    }
    i++;
    
    NSString *str = [self.tagsArr lastObject];
    NSString *temp = [NSString stringWithFormat:@"%@我",str];
    [self.tagsArr addObject:temp];
    [self.tagsV viewWithTagArr:self.tagsArr];
    CGFloat H = self.tagsV.tagsViewH;
    [self.tagsV mas_updateConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(H);
    }];
    
}

- (void)customButton:(UIButton *)sender {
    NSLog(@"点了一下");
}

- (void)showPhoto:(UIButton *)sender {
    PhotoViewController *photoVC = [[PhotoViewController alloc] init];
    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:photoVC];
    [self presentViewController:nav animated:YES completion:nil];
}

- (void)showTabBar:(UIButton *)sender {
    
    TabBarControllerConfig *config = [TabBarControllerConfig shareInstance];
//    [config getTabBarController];
    TabBarViewController *tabBarVC = [config getTabBarController];
    [self presentViewController:tabBarVC animated:YES completion:nil];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
